﻿<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Molgav import</title>
		<meta charset="utf-8">
		<script src="localstore.js"></script>
		<script src="MIDIFile.js"></script>
		<script src="WebAudioFontPlayer.js"></script>
		<script src="0300_LesPaul_sf2_file.js"></script>
		<script src="0280_LesPaul_sf2_file.js"></script>
		<script src="0250_Chaos_sf2_file.js"></script>
		<!--<script src="0170_JCLive_sf2_file.js"></script>-->
		<script src="0170_SBLive_sf2.js"></script>
		<script src="0001_FluidR3_GM_sf2_file.js"></script>
		<script src="0340_Aspirin_sf2_file.js"></script>
		<script src="0390_GeneralUserGS_sf2_file.js"></script>
		<script src="0490_Chaos_sf2_file.js"></script>
		<script src="12835_0_Chaos_sf2_file.js"></script>
		<script src="12838_22_FluidR3_GM_sf2_file.js"></script>
		<script src="12841_26_JCLive_sf2_file.js"></script>
		<script src="12842_26_JCLive_sf2_file.js"></script>
		<script src="12845_26_JCLive_sf2_file.js"></script>
		<script src="12846_26_JCLive_sf2_file.js"></script>
		<script src="12849_26_JCLive_sf2_file.js"></script>
		<script src="12851_26_JCLive_sf2_file.js"></script>
		<script src="set.js"></script>
		<style>
			html, body {
				font-family: 'Gidugu', serif;
				background-color:#000;
				color: #fff;	
				font-size: xx-large;
				text-align: center;
			}
			a {
				color: #99f;
				text-decoration: none;
			}
			h1 {
				text-align: center;
				border-bottom: 1px solid #333;
			}
			.ftr {
				text-align: left;
				border-top: 1px solid #333;
			}
			.cntr {
				text-align: left;
				display: inline-block;
			}
			@font-face {
			  font-family: 'Gidugu';
			  font-style: normal;
			  font-weight: 400;
			  src: local('Gidugu'), url(gidugu.woff2) format('woff2');
			  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215;
			}
		</style>
	</head>
	<body>
		<h1>Import</h1>
		<div class='cntr'>
			<ul id="mnul">
				<li>Import .mid, skip <input type="number" id="skip16" name="skip16" value="0" size="3" /> <small>16th's</small>
					<br/><input type="file" id="filesOpen" name="files[]" multiple  /></li>
				<li><a href="examples.html">Examples</a></li>
			</ul>
		</div>
		<p class="ftr">Return to <a href="index.html">editor</a></p>
		<script>
			function adjustPitch(pitch){
				var p=1*pitch-12*3;
				if(p<0){
					while(p<0){
						p=p+12;
					}
				}
				if(p>=5*12){
					while(p>=5*12){
						p=p-12;
					}
				}
				return p;
			}
			function noDrum(drums,d,b){
				for(var i=0;i<drums.length;i++){
					if(drums[i].drum==d && drums[i].beat==b){
						return false;
					}
				}
				return true;
			}
			function noTone(tones,t,b,p){
				//console.log(tones);
				for(var i=0;i<tones.length;i++){//console.log(tones,t,b,p);
					if(tones[i].track==t && tones[i].beat==b && tones[i].pitch==p){
						return false;
					}
				}
				return true;
			}
			function openSong(evt) {
				console.log("openSong v2",evt);
				//console.log(encodeState());
				var skp=1*document.getElementById("skip16").value;
				
				var fileList = evt.target.files;
				if (fileList.length > 0) {
					var file = fileList.item(0);
					var fileReader = new FileReader();
					fileReader.onload = function (progressEvent) {
						console.log(progressEvent);
						if (progressEvent.target.readyState == FileReader.DONE) {
							var arrayBuffer = progressEvent.target.result;
							var midiFile = new MIDIFile(arrayBuffer);
							console.log(midiFile);
							
							var song = midiFile.parseSong();							
							console.log(song);
							//console.log(midiFile.header.getTicksPerBeat());
							//console.log(midiFile.header.getTimeDivision());
							//console.log(midiFile.header.getSMPTEFrames());
							//console.log(midiFile.header.getTicksPerFrame());
							//console.log(midiFile.header.setSMTPEDivision());
							//console.log(midiFile.header.getTicksPerBeat(),':',midiFile.header.getTimeDivision());
							var ticksPerBeat=480;//midiFile.header.getTicksPerBeat();
							console.log('ticksPerBeat',ticksPerBeat);
							if(midiFile.header.getTimeDivision() === MIDIFile.Header.TICKS_PER_BEAT) {
								ticksPerBeat=midiFile.header.getTicksPerBeat();
								console.log('now ticksPerBeat',ticksPerBeat);
							}
							
							/*if(midiFile.header.getTimeDivision() === MIDIFile.Header.TICKS_PER_BEAT) {
								console.log(midiFile.header.getTicksPerBeat());
							} else {
								console.log(midiFile.header.getSMPTEFrames());
								console.log(midiFile.header.getTicksPerFrame());
							}*/
							var storeTracks = [];//sureArray(readObjectFromlocalStorage('storeTracks'), []);
							for (var t = 0; t < song.tracks.length; t++) {
								var track = song.tracks[t];
								var ins = 4;
								if (track.program + 1 == 31) {
									ins = 0;
								}
								if (track.program + 1 >= 25 && track.program + 1 <= 28) {
									ins = 1;
								}
								if (track.program + 1 >= 17 && track.program + 1 <= 24) {
									ins = 2;
								}
								if (track.program + 1 == 30) {
									ins = 3;
								}
								if (track.program + 1 >= 33 && track.program + 1 <= 38) {
									ins= 5;
								}
								if (track.program + 1 >= 41 && track.program + 1 <= 88) {
									ins = 6;
								}
								if (track.program + 1 >= 39 && track.program + 1 <= 40) {
									ins = 7;
								}
								for (var n = 0; n < track.notes.length; n++) {
									var note = track.notes[n];
									var beat = Math.round(4 * note.tick / ticksPerBeat);
									if (beat >= skp && beat < skp + 256) {
										if (noTone(storeTracks, ins, beat - skp, adjustPitch(note.pitch))) {
											storeTracks.push({
												track: ins,
												beat: beat - skp,
												pitch: adjustPitch(note.pitch),
												length: Math.round(4 * note.tickDuration / ticksPerBeat),
												shift: 0
											});
										}
									}
								}
							}
							saveObject2localStorage('storeTracks', storeTracks);
							var storeDrums = [];//sureArray(readObjectFromlocalStorage('storeDrums'), []);
							for (var t = 0; t < song.beats.length; t++) {
								var track = song.beats[t];
								var ins = 6;
								if(track.n>=35 && track.n<=36){ins=0;}
								if(track.n==41 || track.n==43){ins=1;}
								if(track.n==38 || track.n==40){ins=2;}
								if(track.n==45 || track.n==47 || track.n==48 || track.n==50){ins=3;}
								if(track.n==42 || track.n==44){ins=4;}
								if(track.n==46){ins=5;}
								if(track.n==49){ins=7;}
								for (var n = 0; n < track.notes.length; n++) {
									var note = track.notes[n];
									var beat = Math.round(4 * note.tick / ticksPerBeat);
									if (beat >= skp && beat < skp + 256) {
										if(noDrum(storeDrums,n,beat-skp)){
											storeDrums.push({drum:ins,beat:beat - skp});
										}
									}
								}
							}
							saveObject2localStorage('storeDrums',storeDrums);
							window.location='index.html';
							/*
							var midiParser = new MidiParser(arrayBuffer);
							midiParser.parse();
							console.log('slice from',skp);
							//storeDrums=[];
							var storeDrums = sureArray(readObjectFromlocalStorage('storeDrums'), []);
							for(var b=skp;b<midiParser.songBeatSteps.length;b++){
								if(b>(skp+255))break;
								var beat=midiParser.songBeatSteps[b];
								for(var c=0;c<beat.length;c++){
									var n=6;
									if(beat[c]>=35 && beat[c]<=36){n=0;}
									if(beat[c]==41 || beat[c]==43){n=1;}
									if(beat[c]==38 || beat[c]==40){n=2;}
									if(beat[c]==45 || beat[c]==47 || beat[c]==48 || beat[c]==50){n=3;}
									if(beat[c]==42 || beat[c]==44){n=4;}
									if(beat[c]==46){n=5;}
									if(beat[c]==49){n=7;}
									if(noDrum(storeDrums,n,b-skp)){
										storeDrums.push({drum:n,beat:b-skp});
									}
								}
							}
							saveObject2localStorage('storeDrums',storeDrums);
							//storeTracks=[];
							var storeTracks = sureArray(readObjectFromlocalStorage('storeTracks'), []);
							for(var b=skp;b<midiParser.songTuneSteps.length;b++){
								if(b>(skp+255))break;
								var beat=midiParser.songTuneSteps[b];
								for(var c=0;c<beat.length;c++){
									var note=beat[c];
									var n=4;
									if(beat[c].instrument+1==31){n=0;}
									if(beat[c].instrument+1>=25 && beat[c].instrument+1<=28){n=1;}
									if(beat[c].instrument+1>=17 && beat[c].instrument+1<=24){n=2;}
									if(beat[c].instrument+1==30){n=3;}
									//
									if(beat[c].instrument+1>=33 && beat[c].instrument+1<=38){n=5;}
									if(beat[c].instrument+1>=41 && beat[c].instrument+1<=88){n=6;}
									if(beat[c].instrument+1>=39 && beat[c].instrument+1<=40){n=7;}
									var u=1*note.length;//-1;
									if(u<1)u=1;
									//console.log(note.length,{track:n,beat:b-skp,pitch:adjustPitch(note.pitch),length:u,shift:note.glissando});
									if(noTone(storeTracks,n,b-skp,adjustPitch(note.pitch))){
										storeTracks.push({track:n,beat:b-skp,pitch:adjustPitch(note.pitch),length:u,shift:note.glissando});
									}
								}
							}
							saveObject2localStorage('storeTracks',storeTracks);
							window.location='index.html';
							*/
						} else {
							console.log(progressEvent.target.readyState);
						}
					};
					fileReader.readAsArrayBuffer(file);
				}
			}
			document.getElementById('filesOpen').addEventListener('change', openSong, false);
			var ns = "http://www.w3.org/2000/svg";
			var h = sureArray(readObjectFromlocalStorage('history'), []);
			//console.log(h);
			var bgMode = sureInList(readTextFromlocalStorage('bgMode'), 0, [0,1,2]);
			var bg=modeBackground(bgMode);
			var fg=modeDrumColor(bgMode);
			for(var ss=0;ss<document.styleSheets.length;ss++){
				var stylrSheet=document.styleSheets[ss];
				for(var ru=0;ru<stylrSheet.cssRules.length;ru++){
					var cssRule=stylrSheet.cssRules[ru];
					if(cssRule.selectorText){
						if(cssRule.selectorText.includes('html')){
							cssRule.style.setProperty('background-color',bg,null);
							cssRule.style.setProperty('color',fg,null);
						}
					}
				}
			}
			for(var i=h.length-1;i>=0;i--){
				addEntry(h[i].label, i, h[i].storeDrums, h[i].storeTracks);
			}
			
			function addEntry(label, num, storeDrums, storeTracks) {
				//console.log(storeTracks);
				var svg = entry(label,num);
				for (var i = 0; i < storeDrums.length; i++) {
					if(bgMode==2){
						line(svg, storeDrums[i].beat, 60 + storeDrums[i].drum, storeDrums[i].beat + 1, 60 + storeDrums[i].drum, '#333');
					}else{
						line(svg, storeDrums[i].beat, 60 + storeDrums[i].drum, storeDrums[i].beat + 1, 60 + storeDrums[i].drum, '#fff');
					}
				}
				for (var i = 0; i < storeTracks.length; i++) {
					line(svg, storeTracks[i].beat, 60 - storeTracks[i].pitch, storeTracks[i].beat + 1+storeTracks[i].length, 60 - storeTracks[i].pitch-storeTracks[i].shift, trackInfo[7 - storeTracks[i].track].color);
				}
			}
			function pop(num) {
				addStateToHistory(true);
				removeStateFromHistory(num);
				window.location='index.html';
			}
			function entry(label,num) {
				var ul = document.getElementById('mnul');
				var li = document.createElement('li');
				ul.appendChild(li);
				var a = document.createElement('a');
				li.appendChild(a);
				a.href = "javascript:pop("+num+");";
				a.appendChild(document.createTextNode(label));
				a.appendChild(document.createElement('br'));
				var svg = document.createElementNS(ns, "svg");
				svg.setAttribute('width', 256);
				svg.setAttribute('height', 76);
				a.appendChild(svg);
				var rect = document.createElementNS(ns, 'rect');
				rect.setAttribute('x', 0);
				rect.setAttribute('y', 0);
				rect.setAttribute('width', 256);
				rect.setAttribute('height', 76);
				rect.setAttribute('fill', bg);
				svg.appendChild(rect);
				return svg;
			}
			function line(svg, x1, y1, x2, y2, color) {
				var line = document.createElementNS(ns, 'line');
				line.setAttribute('x1', x1);
				line.setAttribute('y1', y1);
				line.setAttribute('x2', x2);
				line.setAttribute('y2', y2);
				line.setAttribute('stroke', color);
				line.setAttribute('stroke-width', 2);
				line.setAttribute('stroke-linecap', 'round');
				svg.appendChild(line);
			}
		</script>
	</body>
</html>
